﻿---@diagnostic disable: trailing-space
local extension = Package:new("moepack_cards", Package.CardPack)
extension.extensionName = "moepack"

Fk:loadTranslationTable{
  ["moepack_cards"] = "萌包卡牌",
}

local U = require "packages.utility.utility"

--[[

  基本牌

]]--





local defend_skill = fk.CreateActiveSkill{
  name = "defend_skill",
  prompt = "#defend_skill",
  mod_target_filter = Util.TrueFunc,
  target_filter = Util.TargetFilter,
  on_use = function(self, room, cardUseEvent)
    if not cardUseEvent.tos or #TargetGroup:getRealTargets(cardUseEvent.tos) == 0 then
      cardUseEvent.tos = { { cardUseEvent.from } }
    end
  end,
  feasible = function (self, selected, selected_cards, player, card)
    -- 默认选自己还得判一下合法性
    if player and card and #selected == 0 then
      return not player:isProhibited(player, card)
    end
    return true
  end,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    local from = room:getPlayerById(effect.from)
    if to ~= from then
      room:changeShield(to, 1)
    else
      room:addPlayerMark(to, "defend_usedtimes", 1)
      room:changeShield(to, 2)
    end
  end
}
local defend = fk.CreateBasicCard{
  name = "defend",
  suit = Card.Spade,
  number = 5,
  skill = defend_skill,
}
local defendEffect = fk.CreateTriggerSkill{
  name = "defend_effect",
  global = true,
  mute = true,
  priority = 0.01, 
  frequency = Skill.Compulsory,
  events = { fk.TurnStart },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("defend_usedtimes") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = player:getMark("defend_usedtimes")
    room:setPlayerMark(player, "defend_usedtimes", 0)
    room:changeShield(player, -x)
  end,
}
Fk:addSkill(defendEffect)

extension:addCards({
  defend,

  defend:clone(Card.Club, 5),

})


Fk:loadTranslationTable{
  ["defend"] = "守",
	[":defend"] = "基本牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：一名角色<br /><b>效果</b>：若目标角色：不为你，其获得1点护甲；若为你，你获得2点护甲，下回合开始时失去1点护甲。<font color=\"red\"><br>护甲值最多为5</font>",
  ["defend_effect"] = "守",
  ["#defend_skill"] = "令其他角色获得1点护甲，或令自己获得2点护甲，下回合失去1点",
}


--[[

  锦囊牌
  目录：单体，群体，延时

]]--



local exiling_skill = fk.CreateActiveSkill{
  name = "exiling_skill",
  prompt = "#exiling_skill",
  mod_target_filter = function(self, to_select, selected, user)
    return user.id ~= to_select
  end,
  target_filter = Util.TargetFilter,
  target_num = 1,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    if to.dead then return end
    local choices = {"TurnOverAndDraw:::"..to:getLostHp()}
    if to.hp > 0 then table.insert(choices, 1, "loseHp") end
    local choice = room:askForChoice(to, choices, self.name)
    if choice == "loseHp" then
      room:loseHp(to, 1, self.name)
    else
      to:turnOver()
      if not to.dead then
        to:drawCards(to:getLostHp(), self.name)
      end
    end
  end
}
Fk:addSkill(exiling_skill)
local exiling = fk.CreateTrickCard{
  name = "exiling",
  suit = Card.Club,
  number = 10,
  skill = exiling_skill,
}

extension:addCards({
  exiling,
})

Fk:loadTranslationTable{
  ["exiling"] = "流放",
  [":exiling"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：一名其他角色<br /><b>效果</b>：目标角色选一项：1.失去一点体力；2.翻面并摸X张牌（X为其已损失的体力值）。",
  ["exiling_skill"] = "流放",
  ["#exiling_skill"] = "令一名其他角色选择失去1点体力；或翻面并摸牌",
  ["TurnOverAndDraw"] = "翻面并摸 %arg 张牌",
}


local marriage_skill = fk.CreateActiveSkill{
  name = "marriage_skill",
  prompt = "#marriage_skill",
  mod_target_filter = function(self, to_select, selected, user)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return user.gender ~= target.gender
  end,
  target_filter = Util.TargetFilter,
  target_num = 1,
  on_effect = function(self, room, effect)
    local target = room:getPlayerById(effect.to)
    if target.dead then return end
    local from = room:getPlayerById(effect.from)
    for _, p in ipairs({from, target}) do
      if not p.dead then
        if p:isWounded() then
          room:recover{ who = p, num = 1, skillName = self.name, recoverBy = from }
        else
          p:drawCards(1, self.name)
        end
      end
    end
  end
}
Fk:addSkill(marriage_skill)
local marriage = fk.CreateTrickCard{
  name = "marriage",
  suit = Card.Heart,
  number = 9,
  special_skills = { "recast" },
  skill = marriage_skill,
}
extension:addCards({
  marriage,
})

Fk:loadTranslationTable{
  ["marriage"] = "结姻",
  [":marriage"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：一名异性角色<br /><b>效果</b>：你与目标角色各回复1点体力（若未受伤改为摸一张牌）。",
  ["marriage_skill"] = "结姻",
  ["#marriage_skill"] = "与一名异性角色各回复一点体力(未受伤则摸牌)",
}


local ignite_trigger = fk.CreateTriggerSkill{
  name = "#ignite_trigger",
  global = true,
  mute = true,
  priority = 0.01,
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return data.damageType == fk.FireDamage and target:getMark("@!ignite") > 0 and player == target
  end,
  on_use = function(self, event, target, player, data)
    player.room:sendLog{ type = "#IgniteDamage", from = player.id, arg = "ignite", arg2 = data.damage, arg3 = data.damage + 1 }
    data.damage = data.damage + 1
  end,

  refresh_events = {fk.EnterDying},
  can_refresh = function (self, event, target, player, data)
    return data.damage and data.damage.damageType == fk.FireDamage and player == target
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@!ignite", 0)
  end,
}
Fk:addSkill(ignite_trigger)

local ignite_skill = fk.CreateActiveSkill{
  name = "ignite_skill",
  prompt = "#ignite_skill",
  mod_target_filter = function(self, to_select)
    return Fk:currentRoom():getPlayerById(to_select):getMark("@!ignite") == 0
  end,
  target_filter = Util.TargetFilter,
  target_num = 1,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    if to.dead then return end
    room:setPlayerMark(to, "@!ignite", 1)
  end
}
Fk:addSkill(ignite_skill)
local ignite = fk.CreateTrickCard{
  name = "ignite",
  suit = Card.Heart,
  number = 4,
  special_skills = { "recast" },
  skill = ignite_skill,
}
extension:addCards({
  ignite,
})

Fk:loadTranslationTable{
  ["ignite"] = "点燃",
  [":ignite"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：未被点燃的一名角色<br /><b>效果</b>：目标角色受到的火焰伤害+1，直到因火焰伤害进入濒死状态。",
  ["ignite_skill"] = "点燃",
  ["@!ignite"] = "点燃",
  ["#ignite_trigger"] = "点燃",
  ["#IgniteDamage"] = "由于 %arg 的效果，%from 受到的伤害由 %arg2 增加到 %arg3",
  ["#ignite_skill"] = "令一名角色受到火焰伤害+1，直到其濒死",
}


local alive_kingdoms = function (room)
  local kingdoms = {}
  for _, p in ipairs(room.alive_players) do
    table.insertIfNeed(kingdoms, p.kingdom)
  end
  return #kingdoms
end

local seal_ambitionSkill = fk.CreateActiveSkill{
  name = "seal_ambition_skill",
  prompt = "#seal_ambition_skill",
  mod_target_filter = Util.TrueFunc,
  can_use = function(self, player, card)
    return not player:isProhibited(player, card)
  end,
  on_use = function(self, room, cardUseEvent)
    if not cardUseEvent.tos or #TargetGroup:getRealTargets(cardUseEvent.tos) == 0 then
      cardUseEvent.tos = { { cardUseEvent.from } }
    end
  end,
  on_effect = function(self, room, effect)
    local target = room:getPlayerById(effect.to)
    if target.dead then return end
    room:addPlayerMark(target, "seal_ambition_phase")
    target:drawCards(1 + alive_kingdoms(room), "seal_ambition")
  end
}
local seal_ambition = fk.CreateTrickCard{
  name = "seal_ambition",
  suit = Card.Heart,
  number = 1,
  skill = seal_ambitionSkill,
}

extension:addCards({
  seal_ambition,
})
local seal_ambition_trigger = fk.CreateTriggerSkill{
  name = "seal_ambition_trigger",
  global = true,
  --mute = true,
  anim_type = "negative",
  priority = 0.01, 
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    return player:getMark("seal_ambition_phase") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local count = player:getMark("seal_ambition_phase")
    room:setPlayerMark(player, "seal_ambition_phase", 0)
    for _ = 1, count do
      if player.dead or player:isNude() then break end
      local num = alive_kingdoms(room)
      room:askForDiscard(player, num, num, true, self.name, false)
    end
  end,
}
Fk:addSkill(seal_ambition_trigger)

Fk:loadTranslationTable{
  ["seal_ambition"] = "持玺妄尊",
  [":seal_ambition"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：你<br /><b>效果</b>：目标角色摸X+1张牌，当前阶段结束时弃X张牌（X为存活势力数）。",
  ["seal_ambition_trigger"] = "持玺妄尊",
  ["#seal_ambition_skill"] = "摸场上势力数+1张牌，本阶段结束时弃场上势力数张牌",
}


local pitch_campSkill = fk.CreateActiveSkill{
  name = "pitch_camp_skill",
  can_use = Util.GlobalCanUse,
  on_use = Util.GlobalOnUse,
  prompt = "#pitch_camp_skill",
  mod_target_filter = Util.TrueFunc,
  on_effect = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.to)
    local all_choices = { "#pitch_camp_draw2", "#pitch_camp_recover", "#pitch_camp_nullify"}
    local choices = table.simpleClone(all_choices)
    if not target:isWounded() then table.removeOne(choices, "#pitch_camp_recover") end
    local choice = room:askForChoice(target, choices, self.name, nil, false, all_choices)
    if choice == "#pitch_camp_draw2" then
      target:drawCards(2, self.name)
    elseif choice == "#pitch_camp_recover" then
      room:recover({
        who = target,
        num = 1,
        recoverBy = player,
        card = effect.card,
        skillName = self.name,
      })
    else
      local e = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if e then
        local use = e.data[1]
        if use then
          use.nullifiedTargets = TargetGroup:getRealTargets(use.tos)
        end
      end
    end
  end
}

local pitchcamp = fk.CreateTrickCard{
  name = "pitch_camp",
  suit = Card.Diamond,
  number = 12,
  multiple_targets = true,
  skill = pitch_campSkill,
}
extension:addCards({
  pitchcamp,
})


Fk:loadTranslationTable{
  ["pitch_camp"] = "安营扎寨",
  ["pitch_camp_skill"] = "安营扎寨",
  [":pitch_camp"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标：</b>所有角色<br /><b>效果：</b>目标角色选择一项：1.摸两张牌；2.回复一点体力；3.令此牌对未结算的目标无效。",
  ["#pitch_camp_draw2"] = "摸两张牌",
  ["#pitch_camp_recover"] = "回复1点体力",
  ["#pitch_camp_nullify"] = "令此牌对未结算的目标无效",
  ["#pitch_camp_skill"] = "令所有角色依次选择回复体力或摸两张牌，或令此牌无效",
}








--[[

  装备牌
  目录：武器，防具，坐骑，宝具

]]--

local zhijianequip = fk.CreateActiveSkill{
  name = "zhijianequip",
  target_num = 1,
  prompt = "#zhijianequip",
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and Self.id ~= to_select and #selected_cards == 1
    and Fk:currentRoom():getPlayerById(to_select):hasEmptyEquipSlot(Fk:getCardById(selected_cards[1]).sub_type)
  end,
  on_use = function(_, room, effect)
    room:moveCards({
      ids = effect.cards,
      from = effect.from,
      to = effect.tos[1],
      toArea = Card.PlayerEquip,
      moveReason = fk.ReasonPut,
      proposer = effect.from,
    })
  end,
}

Fk:addSkill(zhijianequip)

Fk:loadTranslationTable{
  ["zhijianequip"] = "直谏",
  [":zhijianequip"] = "你可以将手牌中的此装备置入一名其他角色装备区。",
  ["#zhijianequip"] = "将此牌置入一名其他角色的装备区",
}


local tiger_axe_skill = fk.CreateTriggerSkill{
  name = "#tiger_axe_skill",
  anim_type = "offensive",
  attached_equip = "tiger_axe",
  priority = 0.1, 
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and not player:isKongcheng() and data.card.trueName == "slash" 
    and data.firstTarget and U.isOnlyTarget (player.room:getPlayerById(data.to), data, event) 
    and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askForDiscard(player, 1, 1, false, self.name, true, "slash", "#tiger_axe-discard", true)
    if #cards > 0 then
      self.cost_data = cards
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:throwCard(self.cost_data, self.name, player, player)
    data.additionalEffect = (data.additionalEffect or 0) + 1
  end,
}

Fk:addSkill(tiger_axe_skill)

local tiger_axe = fk.CreateWeapon{
  name = "tiger_axe",
  suit = Card.Diamond,
  number = 11,
  attack_range = 2,
  equip_skill = tiger_axe_skill,
  special_skills = { "zhijianequip" },
}

extension:addCards({
  tiger_axe,
})

Fk:loadTranslationTable{
  ["tiger_axe"] = "双虎斧",
  [":tiger_axe"] = "装备牌·武器<br /><b>攻击范围</b>：２<br /><b>武器技能</b>：每回合限一次，每当你使用【杀】指定唯一目标后，你可以弃置一张【杀】令此【杀】额外结算一次。",
  ["#tiger_axe_skill"] = "双虎斧",
  ["#tiger_axe-discard"] = "双虎斧：你可以弃置一张【杀】令此【杀】额外结算一次",
}





local alpaca_skill = fk.CreateTriggerSkill{
  name = "#alpaca_skill",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart , fk.Damaged},
  priority = 0.1,
  mute = true,
  attached_equip = "alpaca",
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return player:hasSkill(self) and player == target and player.phase == Player.Finish and player:isWounded()
    else
      return player:hasSkill(self) and player == target and data.from and data.from ~= player and data.card and data.card.trueName == "slash"
      and table.find(player:getEquipments(Card.SubtypeDefensiveRide), function(cid) return Fk:getCardById(cid).name == "alpaca" end) ~= nil
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      room:notifySkillInvoked(player, self.name, "defensive")
      local choice = room:askForChoice(player, {"draw2" , "recover"}, self.name)
      if choice == "draw2" then
        player:drawCards(2, self.name)
      else
        room:recover({ who = player, num = 1, recoverBy = player, skillName = self.name })
      end
    else
      room:doAnimate("InvokeSkill", { name = self.name, player = player.id, skill_type = "negative" })
      local cid = table.find(player:getEquipments(Card.SubtypeDefensiveRide), function(cid) return Fk:getCardById(cid).name == "alpaca" end)
      if not cid then return end
      if data.from:canMoveCardIntoEquip(cid, false) then
        room:doIndicate(player.id, {data.from.id})
        room:moveCardIntoEquip(data.from, cid, self.name, true, player)
      else
        room:moveCards({
          from = player.id,
          ids = {cid},
          toArea = Card.DiscardPile,
          moveReason = fk.ReasonPutIntoDiscardPile,
        })
      end
    end
  end,
}
Fk:addSkill(alpaca_skill)

local alpaca = fk.CreateDefensiveRide{
  name = "alpaca",
  suit = Card.Club,
  number = 11,
  equip_skill = alpaca_skill,
  special_skills = { "zhijianequip" },
}

extension:addCards({
  alpaca,
})

Fk:loadTranslationTable{
  ["alpaca"] = "羊驼",
  [":alpaca"] = "装备牌·防御坐骑<br /><b>坐骑技能</b>：锁定技，结束阶段开始时，若你已受伤，你选择摸两张牌或回复1点体力。你受到其他角色使用【杀】造成的伤害后，将你装备区的【羊驼】置于其装备区（若无法置入，改为置入弃牌堆）。",
  ["#alpaca_skill"] = "羊驼",
}


local general_volume_skill = fk.CreateTriggerSkill{
  name = "#general_volume_skill",
  anim_type = "drawcard",
  attached_equip = "general_volume",
  events = {fk.EventPhaseStart},
  priority = 0.1,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Finish
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askForChoosePlayers(player, table.map(room.alive_players,Util.IdMapper), 1, 1, "#general_volume-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = tos[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local pile = table.simpleClone(room.draw_pile)
    table.insertTable(pile, room.discard_pile)
    local to = room:getPlayerById(self.cost_data)
    local cards = {}
    for _, cid in ipairs(pile) do
      local card = Fk:getCardById(cid)
      if card.trueName == "slash" then
        if to == player and card.name == "slash" then
          table.insert(cards, cid)
        elseif to ~= player and card.name ~= "slash" then
          table.insert(cards, cid)
        end
      end
    end
    if #cards > 0 then
      room:moveCardTo(table.random(cards), Player.Hand, to, fk.ReasonPrey, self.name, nil, true)
    end
  end,
}
Fk:addSkill(general_volume_skill)

local general_volume = fk.CreateTreasure{
  name = "general_volume",
  suit = Card.Spade,
  number = 3,
  equip_skill = general_volume_skill,
  special_skills = { "zhijianequip" },
}
extension:addCard(general_volume)

Fk:loadTranslationTable{
  ["general_volume"] = "点兵谱",
  ["#general_volume_skill"] = "点兵谱",
  [":general_volume"] = "装备牌·宝物<br/><b>宝物技能</b>：结束阶段，你可以获得一张普通【杀】，或令一名其他角色获得一张非普通【杀】。",
  ["#general_volume-choose"] = "点兵谱：选自己：获得普通【杀】，选其他角色：其获得特殊【杀】",
}


extension:addCard(Fk:cloneCard("fire__slash", Card.Diamond, 2))
extension:addCard(Fk:cloneCard("thunder__slash", Card.Club, 10))

for i = 7, 9 do
  local black_jink = Fk:cloneCard("jink", Card.Club, i)
  black_jink.special_skills = { "emo_zhujian&" }
  extension:addCard(black_jink)
end

extension:addCard(Fk:cloneCard("peach", Card.Diamond, 6))
extension:addCard(Fk:cloneCard("peach", Card.Diamond, 12))

extension:addCard(Fk:cloneCard("nullification", Card.Diamond, 2))
extension:addCard(Fk:cloneCard("nullification", Card.Spade, 10))



return extension
